package example.com.terry.servlet;

import example.com.terry.bean.ScheduleDto;
import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 日程查看接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/queryScheduleServlet")
public class QueryScheduleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String id = req.getParameter("id");

        if (id != null) {
            String sql1 = "select * from tb_schedule where id = '" + id + "'";
            ResultSet resultSet = JDBCUtils.initQuery(sql1);

            try {
                // 日程集合
                // 循环取数据库出查询出来的每条信息结果
                ScheduleDto scheduleDto = new ScheduleDto();
                if (resultSet.next()) {
                    //取出当前这条信息的字段信息
                    String sechId = resultSet.getString("id");
                    String scheduleText = resultSet.getString("schedule_text");
                    String scheduleDate = resultSet.getString("schedule_date");
                    String scheduleDateEnd = resultSet.getString("schedule_end");
                    String type = resultSet.getString("type");

                    scheduleDto.setId(sechId);
                    scheduleDto.setScheduleDate(scheduleDate);
                    scheduleDto.setScheduleEndDate(scheduleDateEnd);
                    scheduleDto.setScheduleText(scheduleText);
                    scheduleDto.setType("0".equals(type) ? "未执行" : "1".equals(type) ? "已执行" : "超时未执行");
                    scheduleDto.setUser(CookieUtils.getUser(req));
                }
                resp.getWriter().println(scheduleDto);
            } catch (Exception e) {

            }
        } else {
            String sql1 = "select * from tb_schedule where  user_id = '" + CookieUtils.getUser(req) + "'";
            ResultSet resultSet = JDBCUtils.initQuery(sql1);

            try {
                // 日程集合
                ArrayList<ScheduleDto> scheduleDtos = new ArrayList<>();
                // 循环取数据库出查询出来的每条信息结果
                while (resultSet.next()) {
                    //取出当前这条信息的字段信息
                    String sechId = resultSet.getString("id");
                    String scheduleText = resultSet.getString("schedule_text");
                    String scheduleDate = resultSet.getString("schedule_date");
                    String scheduleDateEnd = resultSet.getString("schedule_end");
                    String type = resultSet.getString("type");
                    ScheduleDto scheduleDto = new ScheduleDto();
                    scheduleDto.setId(sechId);
                    scheduleDto.setScheduleDate(scheduleDate);
                    scheduleDto.setScheduleEndDate(scheduleDateEnd);
                    scheduleDto.setScheduleText(scheduleText);
                    scheduleDto.setType("0".equals(type) ? "未执行" : "1".equals(type) ? "已执行" : "超时未执行");
                    scheduleDto.setUser(CookieUtils.getUser(req));
                    scheduleDtos.add(scheduleDto);
                }
                resp.getWriter().println(scheduleDtos);
            } catch (Exception e) {

            }
        }

    }
}

